<template>
  <view>
    <view class="input-search">
    	<uni-easyinput prefixIcon="search"  trim="all" v-model="keyword" :styles="styles" :placeholderStyle="placeholderStyle"></uni-easyinput>
		<button @click="search">搜索</button>
    </view>
    <uni-popup ref="popup" :animation="true" type="bottom" :maskClick="true">
    	<view class="popup-content">
    		<view class="pupup-search">
    			<view class="left">类型</view>
    			<view class="right">
    				<uni-data-checkbox style="margin:10rpx 0 0 10rpx" v-model="scan_flag" :localdata="scan_flag_arr"></uni-data-checkbox>
    			</view>
    		</view>
    		<view class="form_list">
    			<view class="input-search"><button @click="closePup">确 定</button></view>
    		</view>
    	</view>
    </uni-popup>
	
    <!-- stripe type="selection" 开启多选 -->
    <uni-table ref="table" type="selection" :loading="loading" emptyText="暂无更多数据" filter-type="search"  @selection-change="selectionChange">
	  <uni-tr>
		<uni-th>
			<button class="close_btn del_btn" @click="toDelete">删除</button>
		</uni-th>
	</uni-tr>
        <uni-tr v-for="(item, index) in card_arr" :key="index" >
			<uni-td width="100%" align="left" class="td-class" >
				<div @click="selectes(item)" @longpress="longPressDelete(index)" class="td_div">
					<!-- <view>
						<text>单号{{ item.FBillNo }}</text>
					</view> -->
					<view class="td_list">
						<text class="label225">物料名称</text>
						<text class="text">{{ item.inv_name }}</text>
					</view>
					<view class="td_list">
						<text class="label225">编码</text>
						<text class="text">{{ item.inv_number }}</text>
					</view>
					<view class="td_list">
						<text class="label225">规格</text>
						<text class="text">{{ item.inv_specification=='NULL'?'':item.inv_specification }}</text>
					</view>
					<view class="td_list">
						<text class="label225">批号</text>
						<text class="text">{{ item.cBatch }}</text>
					</view>
					<view class="td_list">
						<text class="label225">位置</text>
						<text class="text">{{ item.shelves_name }}</text>
					</view>
					<view class="td_list">
						<text  class="label225">盘点状态</text>
						<text class="text font-red">{{ item.scan_flag==1?'已扫码':'待扫码' }}</text> 
					</view>
					<view class="td_list">
						<text  class="label225">盘点时间</text>
						<text class="text">{{ item.scan_datetime }}</text>
					</view>
					<view class="td_list">
						<text class="label225">基本单位</text>
						<text class="text">{{ item.inv_unit }}</text>
					</view>
					<view class="td_list">
						<text class="label225">常用单位</text>
						<text class="text">{{ item.store_unit }}</text>
					</view>
					<view class="td_list">
						<text class="label">账面基本数量</text>
						<text class="text">{{ item.kc_qty }}</text>
						<!-- <text class="label">单位</text>
						<text class="text">{{ item.inv_unit }}</text> -->
					</view>
					<view class="td_list">
						<text class="label">账面常用数量</text>
						<text class="text">{{ item.kc_auxqty }}</text>
					</view>
					<view class="td_list">
						<text  class="label">盘点基本数量</text>
						<text class="text">{{ item.check_qty=='null'?'':item.check_qty }}</text>
					</view>
					<view class="td_list">
						<text  class="label">盘点常用数量</text>
						<text class="text">{{ item.check_auxqty=='null'?'':item.check_auxqty }}</text>
					</view>
					
				</div>
			</uni-td>
        </uni-tr>
    </uni-table>
	
    <view class="uni-pagination-box">
		<uni-pagination show-icon :page-size="pageSize" :current="currentPage"
        :total="total" @change="change" />
	</view>
  </view>
</template>

<script>
// 引入uni-ui组件
import {
  uniPagination,
  uniIcons,
  uniCard,
  uniTable,
  uniTr,
  uniTh,
  uniTd,
  uniEasyinput,uniPopup,uniDataCheckbox
  //uniDataSelect
} from "@dcloudio/uni-ui"

export default {
	components: {
	  uniPagination,
	  uniTable,
	  uniTr,
	  uniTh,
	  uniTd,
	  uniEasyinput,uniPopup,
		uniDataCheckbox
	},
  data() {
    return {
      card_arr: [],//当前页码的表格数据
      pageSize: 15,// 每页数据量
      currentPage: 1,// 当前页
      total: 0,// 数据总量
      loading: false,
	  placeholderStyle:'font-size:17px;',
	  styles:{
	  	borderColor: '#999'
	  },
	  keyword:'',
	  where:'',
	  selectedIndexs:[],//选中
      head:{},
	  
	  scan_flag_arr: [
	  		  {
	  		  	text: '全部',
	  		  	value: 0
	  		  },
	  		{
	  			text: '已盘',
	  			value: 2
	  		},{
	  			text: '待盘',
	  			value: 1
	  		}],
	   scan_flag:0,
    }
  },
  methods: {
	longPressDelete(index){
	  	//长按 删除
	  	this.selectedIndexs = []
	  	this.selectedIndexs.push(index)
	  	this.toDelete()
	},
    // 多选
	selectionChange(e) {
		this.selectedIndexs = e.detail.index
	},
    toDelete() {
		let _this = this;
		let delFlag = true;
      uni.showModal({
          title: '确定要删除记录？',
          success: function (res) {
              if (res.confirm) {
				  if(_this.selectedIndexs.length>0){
					 _this.selectedIndexs.forEach(key=>{
						 if(_this.head.syn_status==1){
							 _this.$Api.msg("已同步，无法删除");
							 _this.$refs.table.clearSelection()
							 delFlag = false;
						 }
					 })
					 
					 if(delFlag){
						//删除本地记录
						uni.showLoading({});
						_this.selectedIndexs.forEach(key=>{
								_this.$Db.deleteTableData('check_kc_item','id',_this.card_arr[key].id)
								_this.$Db.exeSQL("update check_kc set totalnum=totalnum-1 where FBillNo='"+_this.card_arr[key].FBillNo+"'")
								_this.$Api.msg('删除成功','success')
								_this.$refs.table.clearSelection()
						})
						uni.hideLoading()
						_this.getlist()
					 }
				  }
              } else if (res.cancel) {
                  return false;
              }
          }
      });
    },
    selectes(item) {
      /* if (item == '') return
	  uni.navigateTo({
	  	url:'/pages/stockbill/item?type='+this.type,
	  		delta:1,
	  		success() {
	  			uni.$emit("getStockbillItem", item)
	  		}
	  }) */
    },
	// 搜索
	search() {
		this.currentPage = 1;
		this.getlist();
	},
	closePup(){
		this.$refs.popup.close()
		this.search();
	},
    //获取列表
    async getlist() {
		uni.showLoading();
		let _this = this
		this.where = " FBillNo = '"+this.head.FBillNo+"' "
		if(this.keyword!=''){
			this.where += " and (shelves_name like '%"+this.keyword+"%' or inv_name like '%"+this.keyword+"%' or inv_number like '%"+this.keyword+"%' or inv_specification like '%"+this.keyword+"%') "
		}
		
		if(this.scan_flag>0){
			this.where += " and scan_flag="+(this.scan_flag-1).toString()
		}
		
		await this.$Db.queryCount('check_kc_item',this.where).then(res => {
			this.total = res[0].num;//获得总页数
		}).catch(err=>{
			console.log(err)
		})
		
		await this.$Db.queryDataList('check_kc_item',this.currentPage,this.pageSize,this.where).then(res => {
			this.card_arr = res
		}).catch(err=>{
			console.log(err)
		})
		
		uni.hideLoading();
    },
    change(e) {
      this.currentPage = e.current
      this.getlist()
    }
  },
  mounted(e) {
    //this.getlist()
  },
  onPullDownRefresh() {
  	this.search()
  	uni.stopPullDownRefresh();
  },
  onNavigationBarButtonTap(e) {
  		if(e.type=='menu'){
  			//弹窗
  			this.$refs.popup.open()
  		}
  },
  onLoad(e) {
	/* 监听 */
	uni.$once("getCheckItem", (value) => {
		this.head = value
		this.search()
	})
  }
}
</script>